//
//  AnotherDetailViewController.m
//  TakeHer
//
//  Created by Jupiter on 15/6/17.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "AnotherDetailViewController.h"
#import "AnotherCell.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "AnotherTableViewController.h"
#import "MJRefresh.h"
@interface AnotherDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSArray *resultArr;
@property (nonatomic, retain) UITableView *tableView;

@end

@implementation AnotherDetailViewController
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    
    //[self.tbView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    self.tableView.headerPullToRefreshText = @"下拉可以刷新了";
    //self.tableView.headerReleaseToRefreshText = @"1436班加油！";
    self.tableView.headerRefreshingText = @"正在帮你刷新中,请稍等";
    
    self.tableView.footerPullToRefreshText = @"上拉可以加载更多数据了";
    self.tableView.footerReleaseToRefreshText = @"松开马上加载更多数据了";
    self.tableView.footerRefreshingText = @"正在帮你加载中,请稍等";
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    NSLog(@"headerRereshing");
    //所有操作控件的线程必须在主线程中实现
    //创建子线程
    dispatch_queue_t t=dispatch_queue_create("aa", nil);
    dispatch_async(t, ^{
        //执行子线程中的操作
        [NSThread sleepForTimeInterval:2];
        //调用主线程，停止刷新
        
        dispatch_async(dispatch_get_main_queue(), ^{
            //这里边写的任何操作，都是主线程的操作
            
            [self.tableView headerEndRefreshing];
        });
    });
}

- (void)footerRereshing
{
    NSLog(@"footerRereshing");
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.resultArr = [NSArray array];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview: self.tableView];
    [_tableView release];
    [self dataHandle];
   [self setupRefresh];

    
}
-(void)dataHandle
{
    NSString *urlString = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/pois/sights/?sort=default&start=0&latitude=38.883890&longitude=121.545445", [self.dic objectForKey:@"type"], [self.dic objectForKey:@"id"]];
    NSLog(@"%@",urlString);
    // 对字符串进行UTF8转码
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    // 创建对象
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manger GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *result = (NSDictionary *)responseObject;
        
        
        self.resultArr = [result objectForKey:@"items"];
        
        //NSLog(@"result:%@", responseObject);
        [self.tableView reloadData];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
           }];
}

//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    //一共有几个分区
//    return 3;
//    
//}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //每个section中要显示的行数
    return self.resultArr.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //每次tableView将要显示一个cell,都会调用这个方法获取cell.
    
    static NSString *cellId = @"reuse";
    
    //1.检查重用池中是否有cell
    AnotherCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    //2.判断取得的cell是否为空
    if (cell == nil) {
        //如果cell是空的,说明重用池中没有cell,需要我们创建一个
        cell = [[[AnotherCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"reuse"]autorelease];
        //NSLog(@"创建cell");
    }
    
    cell.cityTitle.text = [[self.resultArr objectAtIndex:indexPath.row] objectForKey:@"name"];
    [cell.cityImage setImageWithURL:[NSURL URLWithString:[[self.resultArr objectAtIndex:indexPath.row] objectForKey:@"cover_route_map_cover"]]];
    
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
       return 200;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AnotherTableViewController *anotherTable = [[AnotherTableViewController alloc]init];
    [self.navigationController pushViewController:anotherTable animated:YES];
    
    anotherTable.dic = self.resultArr[indexPath.row];
    
    [anotherTable release];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
